﻿using AutoMapper;
using MDT.LifeSewagePlatform.Conmmon;
using MDT.LifeSewagePlatform.Entity;
using MDT.LifeSewagePlatform.InterfaceService;
using MDT.LifeSewagePlatform.IRepository;
using Microsoft.EntityFrameworkCore;
using System;
using System.Collections.Generic;
using System.Linq;
using System.Text;
using System.Threading.Tasks;

/*********************************************************
 * 命名空间 MDT.LifeSewagePlatform.Service
 * 接口名称 RuleCompanyService
 * 开发人员：-nhy
 * 创建时间：2022/3/31 16:57:35
 * 描述说明：
 * 更改历史：
 * 
 * *******************************************************/
namespace MDT.LifeSewagePlatform.Service
{
    internal class RuleCompanyService:BaseService,IReuleCompanyService
    {
        [Autowired]
        protected IRuleCompanyRepository ruleCompanyRepository { get; set; }
        [Autowired]
        protected IAlaramRuleRepository alaramRuleRepository { get; set; }

        [Autowired]
        protected IMapper mapper { get; set; }
        public RuleCompanyService()
        {

        }
        /// <summary>
        /// 添加
        /// </summary>
        /// <param name="Id"></param>
        /// <returns></returns>
        public async Task<BaseMessage> AddAsync(int Id,int companyID,int userId)
        {
            // 默认是启用的状态
            if (await alaramRuleRepository.AnyAsync(alaramRule => alaramRule.ID.Equals(Id))==false)
            {
                return BadRequest400("添加失败!");
            }
            AlaramRuleEntity alaramRuleEntity =await alaramRuleRepository.FirstOrDefaultAsync(alaramRule=>alaramRule.ID.Equals(Id));

            RuleCompanyEntity ruleCompanyEntity = new RuleCompanyEntity(companyID,alaramRuleEntity.Standard,alaramRuleEntity.DescriptionInfo,alaramRuleEntity.AlaramType,alaramRuleEntity.ComparePattern,true);
            await ruleCompanyRepository.Create(ruleCompanyEntity,userId);
            if (await ruleCompanyRepository.SaveChangesAsync())
            {
                return Ok200("添加成功");
            }
            return BadRequest400("添加失败!");

        }
        /// <summary>
        /// 添加多个
        /// </summary>
        /// <param name="Ids"></param>
        /// <param name="companyID"></param>
        /// <param name="userId"></param>
        /// <returns></returns>
        public async Task<BaseMessage> AddAsync(int [] Ids, int companyID, int userId)
        {
            // 默认是启用的状态
            AlaramRuleEntity [] alaramRuleEntitys = await alaramRuleRepository.Where(alaramRule =>Ids.Contains(alaramRule.ID)).ToArrayAsync();
            foreach (var item in alaramRuleEntitys)
            {
                RuleCompanyEntity ruleCompanyEntity = new RuleCompanyEntity(companyID, item.Standard, item.DescriptionInfo, item.AlaramType, item.ComparePattern, true);
                await ruleCompanyRepository.Create(ruleCompanyEntity, userId);
            }
            if (await ruleCompanyRepository.SaveChangesAsync())
            {
                return Ok200("添加成功");
            }
            return BadRequest400("添加失败!");

        }
        /// <summary>
        /// 更新
        /// </summary>
        /// <param name="Id"></param>
        /// <param name="ruleCompanyDto"></param>
        /// <param name="UserId"></param>
        /// <returns></returns>
        public async Task<BaseMessage> UpdateAsync(int Id,RuleCompanyUpdateDto ruleCompanyDto,int UserId)
        {
            var entity=await ruleCompanyRepository.FirstOrDefaultAsync(rule=>rule.ID.Equals(Id));
            mapper.Map(ruleCompanyDto,entity);
            await ruleCompanyRepository.Update(entity,UserId);
            if (await ruleCompanyRepository.SaveChangesAsync())
            {
                return Ok200("更新成功!");
            }
            return BadRequest400("更新失败!");
        }
        /// <summary>
        /// 
        /// </summary>
        /// <param name="Id"></param>
        /// <returns></returns>
        public async Task<BaseMessage> DeleteAsync(int Id)
        {
            if(await ruleCompanyRepository.AnyAsync(rule => rule.ID.Equals(Id))==false)
            {
                return BadRequest400("删除失败!");
            }
            await ruleCompanyRepository.Delete(rule=>rule.ID.Equals(Id));
            if (await ruleCompanyRepository.SaveChangesAsync())
            {
                return Ok200("删除成功!");
            }
            return BadRequest400("删除失败!");
           
        }
        /// <summary>
        /// 软删除
        /// </summary>
        /// <param name="Id"></param>
        /// <param name="UserId"></param>
        /// <returns></returns>
        public async Task<BaseMessage> SoftDeleteAsync(int Id,int UserId)
        {

            if (await ruleCompanyRepository.AnyAsync(rule => rule.ID.Equals(Id)) == false)
            {
                return BadRequest400("删除失败!");
            }

            await ruleCompanyRepository.SoftDelete(rule => rule.ID.Equals(Id),UserId);
            if (await ruleCompanyRepository.SaveChangesAsync())
            {
                return Ok200("删除成功!");
            }
            return BadRequest400("删除失败!");

        }

        public async Task<BaseMessage> GetRuleCompany(RuleCompanyParameter Params)
        {
            var query = ruleCompanyRepository.GetRuleCompany(t=>true);

            return Ok200("查询成功!",await PageList<RuleCompanyView>.CreateAsync(query,Params));
        }
    }
}
